package com.berje.vkalarm.vksdk.model;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;

import com.berje.vkalarm.vksdk.Api;

public class Group implements Serializable {

	private static final long serialVersionUID = 1L;

	private long gid;
	private String name;
	private String photo;// 50*50
	private Boolean is_closed;
	private Boolean is_member;

	// это новые поля, которых у нас пока нет в базе
	// public String screen_name;
	// public Boolean is_admin;
	private String photo_medium;// 100*100
	private String photo_big;// 200*200
	private String description;
	private String wiki_page;
	private Bitmap photoBitmap;

	public static Group parse(JSONObject o) throws JSONException {
		Group g = new Group();
		g.setGid(o.getLong("gid"));
		g.setName(Api.unescape(o.getString("name")));
		g.setPhoto(o.optString("photo"));
		g.setPhoto_medium(o.optString("photo_medium"));
		g.setPhoto_big(o.optString("photo_big"));
		String is_closed = o.optString("is_closed");
		if (is_closed != null)
			g.setIs_closed(is_closed.equals("1"));
		String is_member = o.optString("is_member");
		if (is_member != null)
			g.setIs_member(is_member.equals("1"));
		g.setDescription(Api.unescape(o.optString("description", null)));
		g.setWiki_page(Api.unescape(o.optString("wiki_page", null)));

		// это новые поля, которых у нас пока нет в базе
		// g.screen_name=o.optString("screen_name");
		// String is_admin=o.optString("is_admin");
		// if(is_admin!=null)
		// g.is_admin=is_admin.equals("1");
		// g.photo_medium = o.getString("photo_medium");
		// g.photo_big = o.getString("photo_big");
		return g;
	}

	public static ArrayList<Group> parseGroups(JSONArray jgroups) throws JSONException {
		ArrayList<Group> groups = new ArrayList<Group>();
		for (int i = 0; i < jgroups.length(); i++) {
			// для метода groups.get первый элемент - количество
			if (!(jgroups.get(i) instanceof JSONObject))
				continue;
			JSONObject jgroup = (JSONObject) jgroups.get(i);
			Group group = Group.parse(jgroup);
			groups.add(group);
		}
		return groups;
	}

	public static Group parseFaveGroup(JSONObject o) throws JSONException {
		Group g = new Group();
		String url = o.optString("url");
		g.setGid(Long.parseLong(url.replace("/club", "")));
		g.setName(Api.unescape(o.optString("title")));
		g.setPhoto_medium(o.optString("image_src"));
		g.setDescription(Api.unescape(o.optString("description", null)));
		return g;
	}

	public String toString() {
		return String.format("Group{gid: %d, name: %s, photo: %s}", gid, name, photo);
	}

	public long getGid() {
		return gid;
	}

	public void setGid(long gid) {
		this.gid = gid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}

	public Boolean getIs_closed() {
		return is_closed;
	}

	public void setIs_closed(Boolean is_closed) {
		this.is_closed = is_closed;
	}

	public Boolean getIs_member() {
		return is_member;
	}

	public void setIs_member(Boolean is_member) {
		this.is_member = is_member;
	}

	public String getPhoto_medium() {
		return photo_medium;
	}

	public void setPhoto_medium(String photo_medium) {
		this.photo_medium = photo_medium;
	}

	public String getPhoto_big() {
		return photo_big;
	}

	public void setPhoto_big(String photo_big) {
		this.photo_big = photo_big;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getWiki_page() {
		return wiki_page;
	}

	public void setWiki_page(String wiki_page) {
		this.wiki_page = wiki_page;
	}

	public Bitmap getPhotoBitmap() {
		return photoBitmap;
	}

	public void setPhotoBitmap(Bitmap photoBitmap) {
		this.photoBitmap = photoBitmap;
	}

}
